<template>
  <el-dialog
    :title="title"
    :visible.sync="addVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @click="handleCancel"
  >
    <el-form :model="menuInfo">
      <el-form-item label="菜单id" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.id" :disabled="!isAdd"></el-input>
      </el-form-item>
      <el-form-item label="菜单名称" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.label"></el-input>
      </el-form-item>
      <el-form-item label="菜单父id" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.parentId"></el-input>
      </el-form-item>
      <el-form-item label="菜单父级名称" :label-width="formLabelWidth">
        <el-select
          v-model="selValue"
          placeholder="请选择"
          @change="handlerChangeParentMenu"
        >
          <el-option :key="0" label="顶级菜单" :value="0"> </el-option>
          <el-option
            v-for="item in ones"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

        <!-- <el-input v-model="menuInfo.parentName"></el-input> -->
      </el-form-item>
      <el-form-item label="授权标识符" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.code"></el-input>
      </el-form-item>
      <el-form-item label="路由名称" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="路由地址" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.url"></el-input>
      </el-form-item>
      <el-form-item label="权限类型" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.type"></el-input>
      </el-form-item>
      <el-form-item label="图标" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.icon"></el-input>
      </el-form-item>
      <el-form-item label="备注" :label-width="formLabelWidth">
        <el-input v-model="menuInfo.remark"></el-input>
      </el-form-item>

      <!--  -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleSave">确 定</el-button>
    </div>
  </el-dialog>
</template>
  
  <script>
import {
  menuInfoByIdApi,
  addMenuApi,
  editMenuApi,
  oneMenuApi,
} from "@/Api/MenuApi";
export default {
  props: ["addVisible", "title"],
  data() {
    return {
      menuInfo: {
        id: "",
        label: "",
        parentId: "",
        parentName: "",
        code: "",
        name: "",
        url: "",
        type: "",
        icon: "",
        remark: null,
      },
      isAdd: true,
      ones: [],
      selValue: "",
      parentMenu: {
        0: "顶级菜单",
      },
      formLabelWidth: "120px",
    };
  },
  mounted() {
    this.loadOneMenus();
  },
  methods: {
    handleCancel() {
      // this.resetForm();
      this.$emit("closeAddDialog");
    },
    //加载菜单信息
    async loadOneMenus() {
      try {
        const res = await oneMenuApi();

        if (res.status == 200) {
          this.ones = res.data.map((item) => {
            this.parentMenu[item.id] = item.label;
            return {
              label: item.label,
              value: item.id,
            };
          });
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 1000,
          });
        }
      } catch (error) {
        console.log(error);
        this.$message({
          message: "系统异常",
          type: "error",
          duration: 1000,
        });
      }
    },
    async handleSave() {
      try {
        let res;
        if (this.isAdd) {
          res = await addMenuApi(this.menuInfo);
        } else {
          res = await editMenuApi(this.menuInfo);
        }

        if (res.status == 200) {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 200,
            onClose: () => {
              this.$emit("closeAddDialog", true);
            },
          });
        } else {
          this.$message({
            message: res.msg,
            type: "error",
            duration: 1000,
          });
        }
      } catch (error) {
        this.$message({
          message: "系统异常",
          type: "error",
          duration: 1000,
        });
      }
    },
    async openDialog(menuId) {
      if (!menuId) {
        this.menuInfo = {
          id: "",
          label: "",
          parentId: "",
          parentName: "",
          code: "",
          name: "",
          url: "",
          type: "",
          icon: "",
          remark: null,
        };
        this.isAdd = true;
        this.selValue = 0;
      } else {
        this.isAdd = false;
        try {
          const res = await menuInfoByIdApi(menuId);
          if (res.status == 200) {
            this.menuInfo = res.data;
            this.selValue = this.menuInfo.parentId;
          } else {
            this.$message({
              message: res.msg,
              type: "error",
              duration: 1000,
            });
          }
        } catch (error) {
          this.$message({
            message: "系统异常",
            type: "error",
            duration: 1000,
          });
        }
      }
    },
    //改变父级元素菜单
    handlerChangeParentMenu(parentId) {
      this.menuInfo.parentId = parentId;
      this.menuInfo.parentName = this.parentMenu[parentId];
    },
  },
};
</script>
  
  <style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>    